
import java.awt.Color;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.StringTokenizer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;

public class State implements KeyListener
{
	TextField tf;
	TextArea jf;
	JLabel l;
	TitledBorder tb;
	JPanel pHacked;
	JFrame frame;
	ImageIcon im1=new ImageIcon("check.jpg");
    ImageIcon im2=new ImageIcon("error.png");
    public State()
    {
    	initializare();
    }
    public void initializare()
    {
    	tf = new TextField(50);
        l=new JLabel();
        jf=new TextArea();
        l.setText("Label");
        TitledBorder tb=new TitledBorder("");

        pHacked = new JPanel();
        tb.setTitle("Insert Command");
        
        pHacked.setBorder(tb);
        pHacked.add(l);
        tf.addKeyListener(this);
        pHacked.add(tf);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new java.awt.GridLayout(2, 1));
        frame.getContentPane().add(pHacked);
        frame.getContentPane().add(jf);
      //  frame.getContentPane().add(pStock);
        frame.setSize(800, 150);
        frame.setVisible(true);
        tf.requestFocus();
    }
    @Override
    public void keyPressed(KeyEvent arg0) 
    {
	// TODO Auto-generated method stub
    }

    @Override
    public void keyReleased(KeyEvent arg0) 
    {
	// TODO Auto-generated method stub	
    	int i=1;
		boolean flag=true;
			//if(arg0.getKeyChar()==';')
			//{
				 StringTokenizer st = new StringTokenizer(tf.getText()," ");
				 String []s=new String[21];
				 for(int k=1;k<=20;k++)
					s[k]="";
				 while(st.hasMoreTokens())
				 {
						 s[i]=st.nextToken();
						 i++;
				 }
				 if(i<3)
				 {
					 l.setIcon(im2);
					 l.setText("Uncomplete command!");
					 l.setForeground(Color.red);
					 return;
				 }
				if(s[1].equals("Create"))
				{
					
					if(s[2].equals("Point"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please give the name of the point!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("At"))
						{
							if(s[5].startsWith("(")&&s[5].endsWith(")")&&s[5].indexOf(",")>1)
							{
								l.setText("");
								l.setIcon(im1);
								jf.append("\nPointName: "+s[3]+"\nPointValue: "+s[5]);
							}
							else
							{
								l.setIcon(im2);
								l.setText("Incorrect parameters for 'Point'!");
								l.setForeground(Color.red);
							}
								
						}
						else 
						{
							l.setIcon(im2);
							l.setText("Incorrect preposition for 'Point'!");
							l.setForeground(Color.red);
						}
							
					}
					else if(s[2].equals("Wall"))
					{
						/*if((jf.getText().indexOf(s[3])>-1)&&(s[3].length()>0))
						{
							l.setText("Wall name already exists!");
							l.setForeground(Color.red);
						}*/
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please give a name to the wall!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("Using"))
						{
							if(i==7)
							{
								l.setText("");
								l.setIcon(im1);
								jf.append("\nWallName: "+s[3]);
								
							}
							else
							{
								l.setIcon(im2);
								l.setText("Please give two points!");
								l.setForeground(Color.red);
							}
						}
						else 
						{
							l.setIcon(im2);
							l.setText("Incorrect preposition for 'Wall'!");
							l.setForeground(Color.red);
						}
					}
					else if(s[2].equals("Room"))
					{
						/*if((jf.getText().indexOf(s[3])>-1)&&(s[3].length()>0))
						{
							l.setText("Room name already exists!");
							l.setForeground(Color.red);
						}*/
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please give a name to the room!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("Using"))
						{
							if(i==9)
							{
								l.setText("");
								l.setIcon(im1);
								jf.append("\nRoomName: "+s[3]);
								
							}
							else
							{
								l.setIcon(im2);
								l.setText("Please give four walls!");
								l.setForeground(Color.red);
							}
						}
						else 
						{
							l.setIcon(im2);
							l.setText("Incorrect preposition for 'Room'!");
							l.setForeground(Color.red);
						}
					}
					else if(s[2].equals("Sensor"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please give a name to the sensor!");
							l.setForeground(Color.red);
						}
						else
						{
							l.setText("");
							l.setIcon(im1);
							jf.append("\nSensorName: "+s[3]);
						}
					}
					/*else if(s[2].equals("Floor"))
					{
						if((jf.getText().indexOf(s[3])>-1))
						{
							l.setText("Floor name already exists!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("Using"))
						{
							for(int j=5;j<i;j++)
							{
								if(jf.getText().indexOf(s[j])<=-1)
									flag=false;
							}
							if(flag==true)
							{
								l.setText("Correct!!");
								l.setForeground(Color.blue);
							}
							else
							{
								l.setText("Some rooms are not defined!");
								l.setForeground(Color.red);
							}
						}
						else 
						{
							l.setText("Incorrect preposition for 'Floor'!");
							l.setForeground(Color.red);
						}
					}*/
					else if(s[2].equals("House"))
					{
						/*if((jf.getText().indexOf(s[3])>-1))
						{
							l.setText("House name already exists!");
							l.setForeground(Color.red);
						}*/
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please give a name to the house!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("Using"))
						{
							if(s[5].length()>0)
							{
								l.setText("");
								l.setIcon(im1);
								jf.append("\nHouseName: "+s[3]);
								
							}
							else
							{
								l.setIcon(im2);
								l.setText("Please give at least one room!");
								l.setForeground(Color.red);
							}
							/*for(int j=5;j<i;j++)
							{
								if(jf.getText().indexOf(s[j])<=-1)
									flag=false;
							}
							if(flag==true)
							{
								l.setText("Correct!!");
								l.setForeground(Color.blue);
								jf.append("\nHouseName: "+s[3]);
							}
							else
							{
								l.setText("Some rooms are not defined!");
								l.setForeground(Color.red);
							}*/
						}
						else 
						{
							l.setIcon(im2);
							l.setText("Incorrect preposition for 'House'!");
							l.setForeground(Color.red);
						}
					}
					else 
					{
						l.setIcon(im2);
						l.setText("Incorrect command argument!");
						l.setForeground(Color.red);
					}
						
					//l.setText("OK");
					//l.setForeground(Color.black);
				}
				else if(s[1].equals("Set_Type_Of"))
				{
					if(s[2].equals("Room"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please specify the room name you want to reset!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("To"))
							{
								l.setText("");
								l.setIcon(im1);
							}
							else
							{
								l.setIcon(im2);
								l.setText("Preposition for Set_Type_Of doesn't exist!");
								l.setForeground(Color.red);
							}
					}
					else if(s[2].equals("Sensor"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please specify the sensor name you want to reset!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("To"))
							{
								l.setText("");
								l.setIcon(im1);
							}
							else
							{
								l.setIcon(im2);
								l.setText("Incorrect preposition for Set_Type_Of!");
								l.setForeground(Color.red);
							}
					}
					else
					{
						l.setIcon(im2);
						l.setText("Object doesn't exist!");
						l.setForeground(Color.red);
					}
					
				}
				else if(s[1].equals("Modify"))
				{
					if(s[2].equals("Attribute"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please specify the attribute name you want to modify!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("Of"))
						{
							if(s[5].equals("Room"))
							{
								if(s[6].length()<1)
								{
									l.setIcon(im2);
									l.setText("Please specify the room name!");
									l.setForeground(Color.red);
								}
								else if(s[7].equals("To"))
								{
									l.setText("");
									l.setIcon(im1);
								}
								else
								{
									l.setIcon(im2);
									l.setText("Incorrect preposition after object!");
									l.setForeground(Color.red);
								}
							}
							else if(s[5].equals("Wall"))
							{
								if(s[6].length()<1)
								{
									l.setIcon(im2);
									l.setText("Please specify the wall name!");
									l.setForeground(Color.red);
								}
								else if(s[7].equals("To"))
								{
									l.setText("");
									l.setIcon(im1);
								}
								else
								{
									l.setIcon(im2);
									l.setText("Incorrect preposition after object!");
									l.setForeground(Color.red);
								}
							}
							else if(s[5].equals("Sensor"))
							{
								if(s[6].length()<1)
								{
									l.setIcon(im2);
									l.setText("Please specify the sensor name!");
									l.setForeground(Color.red);
								}
								else if(s[7].equals("To"))
								{
									l.setText("");
									l.setIcon(im1);
								}
								else
								{
									l.setIcon(im2);
									l.setText("Incorrect preposition after object!");
									l.setForeground(Color.red);
								}
							}
							else
							{
								l.setIcon(im2);
								l.setText("Object doesn't exist!");
								l.setForeground(Color.red);
							}
						}
						else
						{
							l.setIcon(im2);
							l.setText("Preposition for Modify_Attribute doesn't exist!");
							l.setForeground(Color.red);
						}
					}
					else
					{
						l.setIcon(im2);
						l.setText("Argument for Modify_Attribute doesn't exist!");
						l.setForeground(Color.red);
					}
				}
				
				else if(s[1].equals("Drop"))
				{
					if(s[2].equals("Wall"))
					{
						if(jf.getText().indexOf(s[3])>-1)
						{
							l.setText("");
							l.setIcon(im1);
							String str="",sg="WallName: "+s[3];
							int len=0;
							//str = jf.getText(0, jf.getText().indexOf("WallName: "+s[3]));
							len=str.length()+sg.length();
							jf.replaceRange("", jf.getText().indexOf("WallName: "+s[3]),len);
						}
						else
						{
							l.setIcon(im2);
							l.setText("The object doesn't exist!");
							l.setForeground(Color.red);
						}
					}
					else if(s[2].equals("Room"))
					{
						if(jf.getText().indexOf(s[3])>-1)
						{
							l.setText("");
							l.setIcon(im1);
							String str="",sg="RoomName: "+s[3];
							int len=0;
							//str = jf.getText(0, jf.getText().indexOf("RoomName: "+s[3]));
							len=str.length()+sg.length();
							jf.replaceRange("", jf.getText().indexOf("RoomName: "+s[3]),len);
						}
						else
						{
							l.setIcon(im2);
							l.setText("The object doesn't exist!");
							l.setForeground(Color.red);
						}
					}
					else if(s[2].equals("Sensor"))
					{
						if(jf.getText().indexOf(s[3])>-1)
						{
							l.setText("");
							l.setIcon(im1);
							String str="",sg="SensorName: "+s[3];
							int len=0;
							//str = jf.getText(0, jf.getText().indexOf("SensorName: "+s[3]));
							len=str.length()+sg.length();
							jf.replaceRange("", jf.getText().indexOf("SensorName: "+s[3]),len);
						}
						else
						{
							l.setIcon(im2);
							l.setText("The object doesn't exist!");
							l.setForeground(Color.red);
						}
					}
					else if(s[2].equals("House"))
					{
						if(jf.getText().indexOf(s[3])>-1)
						{
							l.setText("");
							l.setIcon(im1);
							String str="",sg="HouseName: "+s[3];
							int len=0;
							//str = jf.getText(0, jf.getText().indexOf("HouseName: "+s[3]));
							len=str.length()+sg.length();
							jf.replaceRange("", jf.getText().indexOf("HouseName: "+s[3]),len);
						}
						else
						{
							l.setIcon(im2);
							l.setText("The object doesn't exist!");
							l.setForeground(Color.red);
						}
					}
					else
					{
						l.setIcon(im2);
						l.setText("Argument for Drop doesn't exist!");
						l.setForeground(Color.red);
					}
				}
				else if(s[1].equals("Add"))
				{
					
				}
				else if(s[1].equals("Bind"))
				{
					if(s[2].equals("Sensor"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please specify the sensor name you want to bind!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("To"))
							{
								if(s[5].length()<1)
								{
									l.setIcon(im2);
									l.setText("Please specify the object name you want to bind to!");
									l.setForeground(Color.red);
								}
								else
								{
									l.setText("");
									l.setIcon(im1);
								}
							}
							else
							{
								l.setIcon(im2);
								l.setText("Incorrect preposition!");
								l.setForeground(Color.red);
							}
					}
					else
					{
						l.setIcon(im2);
						l.setText("Object doesn't exist!");
						l.setForeground(Color.red);
					}
				}
				else if(s[1].equals("Unbind"))
				{
					if(s[2].equals("Sensor"))
					{
						if(s[3].length()<1)
						{
							l.setIcon(im2);
							l.setText("Please specify the sensor name you want to unbind!");
							l.setForeground(Color.red);
						}
						else if(s[4].equals("From"))
							{
								if(s[5].length()<1)
								{
									l.setIcon(im2);
									l.setText("Please specify the object name you want to bind from!");
									l.setForeground(Color.red);
								}
								else
								{
									l.setText("");
									l.setIcon(im1);
								}
							}
							else
							{
								l.setIcon(im2);
								l.setText("Incorrect preposition!");
								l.setForeground(Color.red);
							}
					}
					else
					{
						l.setIcon(im2);
						l.setText("Object doesn't exist!");
						l.setForeground(Color.red);
					}
				}
				else 
				{
					l.setIcon(im2);
					l.setText("Incorrect command!");
					l.setForeground(Color.red);
				}
				/*else
				{
					l.setText("Incorrect command!");
					l.setForeground(Color.red);
				}
				if(s2.equalsIgnoreCase("point"))
				{
					l.setText("OK");
					l.setForeground(Color.black);
				}
				else
				{
					l.setText("Incorrect arguments!");
					l.setForeground(Color.red);
				}
				if(s4.equalsIgnoreCase("at"))
				{
					l.setText("OK");
					l.setForeground(Color.black);
				}
				else
				{
					l.setText("Incorrect preposition!");
					l.setForeground(Color.red);
				}
				if(s5.startsWith("(")&&s5.endsWith(")"))
				{
					l.setText("OK");
					l.setForeground(Color.black);
				}
				else
				{
					l.setText("Incorrect form values!");
					l.setForeground(Color.red);
				}*/

				
			//}
    }

    @Override
    public void keyTyped(KeyEvent arg0) 
    {
	// TODO Auto-generated method stub
    }
  public static void main(String[] args) 
  {

    State wh=new State();
  }
}